package com.example.dzbsh.service;

import com.example.dzbsh.dao.ZhenbieQingkuangMapper;
import com.example.dzbsh.model.Jiashu;
import com.example.dzbsh.model.Renyuan;
import com.example.dzbsh.model.ZhenbieQingkuang;
import com.example.dzbsh.model.ZhenbieQingkuangExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ZhenbieQingkuangService {

    @Resource
    ZhenbieQingkuangMapper ddZhenbieQingkuangMapper;
    @Resource
    RenyuanService ddRenyuanService;
    @Resource
    JiashuService ddJiashuService;

    public  Integer countByShifouGuifan(String ppShifouGuifan){
        Object mmCount = ddZhenbieQingkuangMapper.countByShifouGuifan(ppShifouGuifan);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Integer countByJiashuId(String ppJiashuId) {
        Object mmCount = ddZhenbieQingkuangMapper.countByJiashuId(ppJiashuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<String> findAllRenyuanid(String ppDeptId, String ppTrueName,int ppPageIndex, int ppPageSize){
        String mmTrueName = "%"+ppTrueName+"%";
        return ddZhenbieQingkuangMapper.findAllRenyuanid(ppDeptId,mmTrueName,ppPageIndex,ppPageSize);
    }

    public Integer getCustomCountByvalid(String ppDeptId, String ppTrueName) {
        String mmTrueName = "%"+ppTrueName+"%";
        Object mmCount = ddZhenbieQingkuangMapper.countAllRenyuanid(ppDeptId,mmTrueName);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<Map<String,Object>> findAllRenyuan(String ppDeptId, String ppTrueName,int ppPageIndex, int ppPageSize){
        List<Map<String,Object>> mmList = new ArrayList<>();
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        List<String> mmRenyuanIdList = findAllRenyuanid(ppDeptId,ppTrueName,mmStartIndex,ppPageSize);
        for (int i = 0; i < mmRenyuanIdList.size(); i++) {
            String mmRenyuanId = mmRenyuanIdList.get(i)==null?"":mmRenyuanIdList.get(i);
            if (!"".equals(mmRenyuanId)){
                Map<String,Object> mmMap = findOneRenyuan(mmRenyuanId);


                mmList.add(mmMap);
            }

        }
        return mmList;
    }

    public Map<String,Object> findOneRenyuan(String ppRenyuanId){
        Renyuan mmRenyuan = ddRenyuanService.findOne(ppRenyuanId);
        int mmTuichufangshi = findTuichufangshiByRenyuanId(ppRenyuanId);


        String mmXingming= mmRenyuan.getTruename()==null?"":mmRenyuan.getTruename();
        String mmChushengnianyue = mmRenyuan.getChusheng()==null?"":mmRenyuan.getChusheng().toString();
        int mmNianling =0;
        Calendar cal = Calendar.getInstance();

        if (!"".equals(mmChushengnianyue)){


            int yearNow = cal.get(Calendar.YEAR);  //当前年份
            int monthNow = cal.get(Calendar.MONTH);  //当前月份

            cal.setTime(mmRenyuan.getChusheng());
            int yearChusheng = cal.get(Calendar.YEAR);  //出生年份
            int monthChusheng = cal.get(Calendar.MONTH);  //出生月份

            // 出生年份大于出生当前月份则年龄减一
            mmNianling = yearNow-yearChusheng;
            if (monthNow<monthChusheng){
                mmNianling = mmNianling-1;
            }
            mmChushengnianyue = yearChusheng +"年"+monthChusheng+"月";
        }

        String mmJunxiandengji = mmRenyuan.getJunxiandengjitext()==null?"":mmRenyuan.getJunxiandengjitext();
        String mmJunxianshijian = mmRenyuan.getJunxianshijian()==null?"":mmRenyuan.getJunxianshijian().toString();
        String mmZhiwucengji = mmRenyuan.getZhiwucengjitext()==null?"":mmRenyuan.getZhiwucengjitext();
        String mmZhiwushijian = mmRenyuan.getZhiwushijian()==null?"":mmRenyuan.getZhiwushijian().toString();
        String mmBuzhibie = mmRenyuan.getBuzhibie()==null?"":mmRenyuan.getBuzhibie();
        String mmDeptName = mmRenyuan.getDeptname()==null?"":mmRenyuan.getDeptname();


        if (!"".equals(mmJunxianshijian)){
            cal.setTime(mmRenyuan.getJunxianshijian());
            mmJunxianshijian = cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月";
        }

        if (!"".equals(mmZhiwushijian)){
            cal.setTime(mmRenyuan.getZhiwushijian());
            mmZhiwushijian = cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月";
        }


        Map<String,Object> mmMap = new HashMap<>();
        mmMap.put("xingming",mmXingming);
        mmMap.put("xingming2",mmXingming);
        mmMap.put("chushengnianyue",mmChushengnianyue);
        mmMap.put("nianling",mmNianling);
        mmMap.put("junxiandengji",mmJunxiandengji);
        mmMap.put("junxianshijian",mmJunxianshijian);
        mmMap.put("zhiwucengji",mmZhiwucengji);
        mmMap.put("zhiwushijian",mmZhiwushijian);
        mmMap.put("buzhibie",mmDeptName+mmBuzhibie);
        mmMap.put("renyuanid",ppRenyuanId);
        mmMap.put("damweiming",mmDeptName);
        mmMap.put("tuichufangshi",mmTuichufangshi);

        return mmMap;
    }


    public List<String> findJiashuByRenyuanIdAndGuanxi(String ppRenyuanId, String ppGuanxi){
        return ddZhenbieQingkuangMapper.findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,ppGuanxi);
    }

    public List<String> selectZhenbieIdList(){
        return ddZhenbieQingkuangMapper.selectZhenbieIdList();
    }

    public List<ZhenbieQingkuang> findValidByRenyuanId(String ppRenyuanId){
        ZhenbieQingkuangExample mmExample = new ZhenbieQingkuangExample();
        mmExample.createCriteria().andRenyuanidEqualTo(ppRenyuanId).andIsvalidEqualTo(1);
        return ddZhenbieQingkuangMapper.selectByExample(mmExample);
    }

    public List<String> findZhenbieXiangqingByRenyuanId(String ppRenyuanId){
        List<ZhenbieQingkuang> mmZhenbieQingkuangList = findValidByRenyuanId(ppRenyuanId);
        List<String> mmReturnList = new ArrayList<>();
        for (int i = 0; i < mmZhenbieQingkuangList.size(); i++) {
            ZhenbieQingkuang mmZhenbieQingkuang = mmZhenbieQingkuangList.get(i);
            String mmXiangqing = "";
            int mmXuhao = i+1;
            mmXiangqing += "&nbsp;&nbsp;&nbsp;&nbsp;"+mmXuhao+".";
            String mmJiashuId = mmZhenbieQingkuang.getJiashuid() ==null?"":mmZhenbieQingkuang.getJiashuid();
            Jiashu mmJiashu = ddJiashuService.findOne(mmJiashuId);
            if (mmJiashu!=null){
                String mmJiashuName = mmJiashu.getName()==null?"":mmJiashu.getName();
                if (!"".equals(mmJiashuName)){
                    mmXiangqing += mmJiashuName+",";
                }
            }




            String mmJingshangleixing = mmZhenbieQingkuang.getLeixing()==null?"":mmZhenbieQingkuang.getLeixing();
            String mmRenjiaochuzie = mmZhenbieQingkuang.getRenjiaochuzie()==null?"":mmZhenbieQingkuang.getRenjiaochuzie();
            String mmQiyeName = mmZhenbieQingkuang.getQiyename() ==null?"":mmZhenbieQingkuang.getQiyename();
            String mmZhiwu = mmZhenbieQingkuang.getZhiwutext() ==null?"":mmZhenbieQingkuang.getZhiwutext();
            String mmZhucedi = mmZhenbieQingkuang.getQiyesuozaidi() ==null?"":mmZhenbieQingkuang.getQiyesuozaidi();
            String mmJingyingfanwei = mmZhenbieQingkuang.getJingyingfanwei() ==null?"":mmZhenbieQingkuang.getJingyingfanwei();
            String mmJingyingzhuangtai = mmZhenbieQingkuang.getJingyingzhuangtaitext() ==null?"":mmZhenbieQingkuang.getJingyingzhuangtaitext();

            if ("1".equals(mmJingshangleixing)){
                if ("".equals(mmRenjiaochuzie)){
                    if ("经营者".equals(mmZhiwu)||"开办人".equals(mmZhiwu)||"合伙人".equals(mmZhiwu)){
                        mmXiangqing += "为"+mmQiyeName+mmZhiwu;
                    }else {
                        mmXiangqing += "受聘担任"+mmQiyeName+mmZhiwu ;
                    }

                }else {
                    mmXiangqing += "投资"+mmQiyeName+",认缴出资额"+mmRenjiaochuzie+"万元";
                    if ("经营者".equals(mmZhiwu)||"开办人".equals(mmZhiwu)||"合伙人".equals(mmZhiwu)){
                        mmXiangqing += "为"+mmQiyeName+mmZhiwu;
                    }else {
                        mmXiangqing+="，在企业担任"+mmZhiwu+"职务";
                    }

                }
                if (!"".equals(mmZhucedi)){
                    mmXiangqing+=",企业注册地为" +mmZhucedi;
                }
                if (!"".equals(mmJingyingfanwei)){
                    mmXiangqing+=",经营范围是"+mmJingyingfanwei;
                }
                if (!"".equals(mmJingyingzhuangtai)){
                    mmXiangqing+=",当前登记状态为"+mmJingyingzhuangtai;
                }
            }else {
                if ("经营者".equals(mmZhiwu)||"开办人".equals(mmZhiwu)||"合伙人".equals(mmZhiwu)){
                    mmXiangqing += "为"+mmQiyeName+mmZhiwu;
                }else {
                    mmXiangqing += "在"+mmQiyeName+"从业";
                }
                if (!"".equals(mmRenjiaochuzie)){
                    mmXiangqing+=",认缴出资额"+mmRenjiaochuzie+"万元";
                }
                if (!"经营者".equals(mmZhiwu)&&!"开办人".equals(mmZhiwu)&&!"合伙人".equals(mmZhiwu)){
                    mmXiangqing+=",担任"+mmZhiwu+"职务";
                }
                if (!"".equals(mmZhucedi)){
                    mmXiangqing+=",该机构所在地为" +mmZhucedi;
                }
                if (!"".equals(mmJingyingzhuangtai)){
                    mmXiangqing+=",经营状态为"+mmJingyingzhuangtai;
                }
            }


            String mmQianwushangpin = mmZhenbieQingkuang.getQianwushangpinhuofuwuname() ==null?"":mmZhenbieQingkuang.getQianwushangpinhuofuwuname();
            String mmQianwujiaoyi = mmZhenbieQingkuang.getQianwujiaoyiduixiangname() ==null?"":mmZhenbieQingkuang.getQianwujiaoyiduixiangname();

            if (!"".equals(mmQianwushangpin)){
                mmXiangqing +=",近三年排在前5名的销售商品或服务名称是"+mmQianwushangpin;
            }
            if (!"".equals(mmQianwujiaoyi)){
                mmXiangqing +=",近三年排在前5名的交易对象名称是"+mmQianwujiaoyi;
            }
            int mmShifouzhijieguanxi = mmZhenbieQingkuang.getShifouyouzhijieguanxi() ==null?0:mmZhenbieQingkuang.getShifouyouzhijieguanxi();
            if (mmShifouzhijieguanxi==1){
                mmXiangqing +=",与干部所在单位（系统）直接发生经济利益关系。";
            }else {
                mmXiangqing +=",未与干部所在单位（系统）直接发生经济利益关系。";
            }

            mmReturnList.add(mmXiangqing);
        }

        return mmReturnList;

    }

    public Map<String,Object> findAllJiashuByRenyuan(String ppRenyuanId){
        // 配偶家属
        List<String> mmPeiouList=findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"1");
        mmPeiouList.addAll(findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"2"));

        // 子女家属
        List<String> mmZinvList=findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"3");
        mmZinvList.addAll(findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"4"));

        // 子女配偶家属
        List<String> mmZinvpeiouList=findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"5");
        mmZinvpeiouList.addAll(findJiashuByRenyuanIdAndGuanxi(ppRenyuanId,"6"));

        String mmShifouguifan ="否";

        if (mmPeiouList.size()>0||mmZinvList.size()>0||mmZinvpeiouList.size()>0){
            mmShifouguifan = "是";
        }

        String mmPeious = "";
        String mmZinvs = "";
        String mmZinvpeious = "";

        for (int i = 0; i < mmPeiouList.size(); i++) {
            mmPeious += mmPeiouList.get(i);
            if (i!=mmPeiouList.size()-1){
                mmPeious += "、";
            }
        }

        for (int j = 0; j < mmZinvList.size(); j++) {
            mmZinvs += mmZinvList.get(j);
            if (j!=mmZinvList.size()-1){
                mmZinvs += "、";
            }
        }

        for (int k = 0; k < mmZinvpeiouList.size(); k++) {
            mmZinvpeious += mmZinvpeiouList.get(k);
            if (k!=mmZinvpeiouList.size()-1){
                mmZinvpeious += "、";
            }
        }


        Map<String, Object> mmMap = new HashMap<String, Object>();
        mmMap.put("Peiou", mmPeious);
        mmMap.put("Zinv", mmZinvs);
        mmMap.put("Zinvpeiou", mmZinvpeious);
        mmMap.put("shifouguifan", mmShifouguifan);

        return mmMap;
    }

    public int findTuichufangshiByRenyuanId(String ppRenyuanId){
        List<ZhenbieQingkuang> mmBenrentuichuList = findbenrentuichuByRenyuanId(ppRenyuanId);
        List<ZhenbieQingkuang> mmTuichujingshangList = findtuichujingshangByRenyuanId(ppRenyuanId);

        int mmResult = 0;

        if (mmBenrentuichuList.size()>0){
            mmResult = 1;
            // 全部勾选为本人退出
           /* if (mmTuichujingshangList.size()==0){

            }*/
        }else{

            // 全部勾选为退出经商
            if (mmTuichujingshangList.size()>0){
                mmResult = 2;
            }
        }

        return mmResult;
    }

    List<ZhenbieQingkuang> findByJingshangId(String ppJingshangId){
        ZhenbieQingkuangExample mmExample = new ZhenbieQingkuangExample();
        mmExample.createCriteria().andIsvalidEqualTo(1).andJingshangidEqualTo(ppJingshangId);
        return ddZhenbieQingkuangMapper.selectByExample(mmExample);
    };

    List<ZhenbieQingkuang> findbenrentuichuByRenyuanId(String ppRenyuanId){
        ZhenbieQingkuangExample mmExample = new ZhenbieQingkuangExample();
        mmExample.createCriteria().andIsvalidEqualTo(1).andRenyuanidEqualTo(ppRenyuanId).andTuichuqingkuangEqualTo(1);
        return ddZhenbieQingkuangMapper.selectByExample(mmExample);
    };

    List<ZhenbieQingkuang> findtuichujingshangByRenyuanId(String ppRenyuanId){
        ZhenbieQingkuangExample mmExample = new ZhenbieQingkuangExample();
        mmExample.createCriteria().andIsvalidEqualTo(1).andRenyuanidEqualTo(ppRenyuanId).andTuichuqingkuangEqualTo(2);
        return ddZhenbieQingkuangMapper.selectByExample(mmExample);
    };

    public Map<String,Object> Daochurending(String ppRenyuanID){
        Map<String,Object> mmRenyuanMap = findOneRenyuan(ppRenyuanID);
        Map<String,Object> mmJiashuMap = findAllJiashuByRenyuan(ppRenyuanID);
        List<String> mmXiangqingList = findZhenbieXiangqingByRenyuanId(ppRenyuanID);
        int mmTuichufangshi = findTuichufangshiByRenyuanId(ppRenyuanID);

        String mmZhenbieXiangqing = "";

        for (int i = 0; i < mmXiangqingList.size(); i++) {
            String mmXiangqing = mmXiangqingList.get(i);
            mmZhenbieXiangqing += "\n"+mmXiangqing;
        }

        Map<String,Object> mmResultMap = new HashMap<>();
        mmResultMap.putAll(mmRenyuanMap);
        mmResultMap.putAll(mmJiashuMap);
        mmResultMap.put("xiangqing",mmZhenbieXiangqing);
        mmResultMap.put("tuichufangshi",mmTuichufangshi);
        return mmResultMap;
    }

    public Integer getCountByzhuangtaiAndRenyuanId(String ppRenyuanId) {
        Object mmCount = ddZhenbieQingkuangMapper.getCountByzhuangtaiAndRenyuanId(ppRenyuanId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getCountByzhuangtaiAndRenyuanIdByGuifan(String ppRenyuanId) {
        Object mmCount = ddZhenbieQingkuangMapper.getCountByzhuangtaiAndRenyuanIdByGuifan(ppRenyuanId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<Map<String,Object>> findByJiashuId(String ppJiashuId){
        return ddZhenbieQingkuangMapper.findByJiashuId(ppJiashuId);
    }
    public List<Map<String, Object>> findByJiashuIdByGuifan(String ppjiashuID){
        return ddZhenbieQingkuangMapper.findByJiashuIdByGuifan(ppjiashuID);
    }
    public boolean delete(String ppId){
        return ddZhenbieQingkuangMapper.deleteByPrimaryKey(ppId)>=0;
    }

    public boolean  add(ZhenbieQingkuang ppZhenbieQingkuang){
        return ddZhenbieQingkuangMapper.insert(ppZhenbieQingkuang)==1;
    }
}
